<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
  <th:block th:include="include :: header('修改系统公告')"/>
  <th:block th:include="include :: summernote-css" />
</head>
<body style="background-color: #F4F6FA">
<div class="main-content" style="margin: 0;padding: 0">

  <form id="form-user-add" class="form-horizontal" th:object="${saffiche}" style="background-color: #F4F6FA">
    <input type="hidden" th:field="*{afficheId}" />
    <input type="hidden" th:field="*{flagInt}"/>
    <input type="hidden" th:field="*{flagString}"/>
    <input type="hidden" th:field="*{spareInt}"/>
    <input type="hidden" th:field="*{spareString}"/>
    <input type="hidden" th:field="*{readTimes}"/>
    <input type="hidden" th:field="*{objDept}" id="objDept"/>
    <input type="hidden" th:field="*{objUser}"/>
    <input type="hidden" th:field="*{attachName}"/>
    <input type="hidden" th:field="*{attachPath}"/>

    <div class="col-sm-12" style="display: flex;justify-content: center">
      <div class="col-sm-10 entering-page">
        <!--系统公告-->
        <div class="col-sm-12">
          <!--标题、类别-->
          <div class="form-group col-sm-12" style="margin-top:10px;">
            <!--工单编号-->
            <label class="col-sm-2 control-label entering-pd is-required">标题：</label>
            <div class="col-sm-4">
              <input th:field="*{ntcTitle}" placeholder="请输入公告标题" class="form-control" type="text"
                     required="required">
            </div>
            <label class="col-sm-2 control-label entering-pd is-required">类别：</label>
            <div class="col-sm-2">
<!--              <select name="sortCode" id="sortCode"-->
<!--                      th:with="ntcTypes=${@commonServiceImpl.findNtcTypes()}"-->
<!--                      onchange="dropChgSendTextOnly(this,'sortName');"-->
<!--                      class="form-control formSelect">-->
<!--                <th:block th:each="item : ${ntcTypes}">-->
<!--                  <option th:text="${item['CSText']}" th:value="${item['CSValue']}"-->
<!--                          th:selected="${item['CSValue'] == saffiche.sortCode}"></option>-->
<!--                </th:block>-->
<!--                <input type="hidden" th:field="*{sortName}" id="sortName"/>-->
<!--              </select>-->
              <select name="sortCode" id="sortCode"
                      onchange="dropChgSendTextOnly(this,'sortName');"
                      class="form-control formSelect">
                <option value="-1">请选择</option>
                <option value="11" th:selected="${'11' == saffiche.sortCode}">普通</option>
                <option value="12" th:selected="${'12' == saffiche.sortCode}">重要</option>
                <option value="21" th:selected="${'21' == saffiche.sortCode}">紧急</option>
              </select>
              <input type="hidden" name="sortName" id="sortName" value="普通"/>
            </div>
          </div>
          <!--通知内容-->
          <div class="form-group col-sm-12">
            <label class="col-sm-2 control-label entering-pd">通知内容：</label>
            <div class="col-sm-10">
              <div class="ibox float-e-margins">
                <div class="ibox-content no-padding">
                  <textarea th:field="*{ntcContent}" class="summernote"></textarea>
                </div>
              </div>
            </div>
          </div>
          <!--来自、发布时间-->
          <div class="form-group col-sm-12">
            <!--来自-->
            <label class="col-sm-2 control-label  entering-pd">来自：</label>
            <div class="col-sm-4">
              <input th:field="*{fromDept}" class="form-control " type="text" />
              <input type="hidden" th:field="*{issueUser}" />
            </div>
            <!--发布时间-->
            <label class="col-sm-2 control-label  entering-pd">发布时间：</label>
            <div class="col-sm-4">
              <input th:field="*{issueTime}" class="form-control " type="text"/>
            </div>
            <div class="input-group-append " style="position: absolute;top: 10px;right: 40px;">
              <span class="glyphicon glyphicon-calendar "></span>
            </div>
          </div>
          <!--责任部门、联系电话-->
          <div class="form-group col-sm-12" id="tellTo">
            <label class="col-sm-2 control-label entering-pd">通知对象：</label>
            <div class="col-sm-10">
                <div class="col-sm-4" id="allIn">
                    <label class="check-box">
                        <input type="checkbox" style="zoom: 120%;" name="checkAll" id="chkAll"/>
                        <span class="spLBlue entering-pd">全选</span>
                    </label>
                </div>
                <div th:if="${@sysRoleServiceImpl.selectRoleAll().size() == 0}">
                    <div class="col-sm-4" style="margin-top:-3px;">
                        <input type="text" id="deptSearch" name="search" class="form-control" placeholder="请输入需要查找的内容">
                    </div>
                    <div class="col-sm-1" style="margin-left:-25px;">
                        <button type="button" class="btn btn-sm btn-info" id="dept-search">查找</button>
                    </div>
                </div>
                <div class="check-box-table-cell col-sm-12 find-list">
                    <div th:if="${@sysRoleServiceImpl.selectRoleAll().size() != 0}">
                        <table cellSpacing="1" cellPadding="1" width="100%" border="0"  th:with="roles=${@sysRoleServiceImpl.selectRoleAll()}">
                            <th:block th:each="entry,stat: ${roles}">
                                        <span th:if="${stat.index % 4 == 0 || stat.index == 0}">
                                            <tr>
                                        </span>
                                <td>
                                    <input name="roleIds"
                                           th:value="${entry.roleId}"
                                           th:text="${entry.roleName}"
                                           th:checked="${#strings.containsIgnoreCase(saffiche.objRole,entry.roleId)}"
                                           style="margin-right:3px" type="checkbox" class="checkbox-inline"/>
                                </td>
                            </th:block>
                        </table>
                        <input type="hidden" name="tell" value="1"/>
                    </div>
                    <div th:if="${@sysRoleServiceImpl.selectRoleAll().size() == 0}">
                        <div>
                            <ul class="deptTransfer-ul" th:with="allDepts=${@commonServiceImpl.allDepartment('__${deptId}__')}">
                                <li class="deptTransfer-li" th:each="entry:${allDepts}">
                                    <label>
                                        <input name="roleIds"
                                               th:value="${entry.noUnit}"
                                               th:text="${entry.deptShort}"
                                               th:checked="${#strings.containsIgnoreCase(saffiche.objDept,entry.noUnit)}"
                                               style="margin-right:3px" type="checkbox" class="checkbox-inline"/>
                                    </label>
                                </li>
                            </ul>
                        </div>
                        <input type="hidden" name="tell" value="2"/>
                    </div>
                    <input type="hidden" id="objRole" name="objRole" th:field="*{objRole}"/>
                </div>
            </div>
          </div>
          <!--发布、置顶-->
          <div class="form-group col-sm-12">
            <label class="col-sm-2 control-label entering-pd">发布：</label>
            <div class="col-sm-4">
              <label class="check-box">
                <input type="radio" name="issueFlag" value="1" th:checked="*{issueFlag == 1}" />是
              </label>
              <label class="check-box">
                <input type="radio" name="issueFlag" value="0" th:checked="*{issueFlag == 0}"/>否
              </label>
            </div>
            <label class="col-xs-2 control-label entering-pd">置顶：</label>
            <div class="col-xs-4">
              <label class="check-box">
                <input type="radio" name="topFlag" value="1" th:checked="*{topFlag == 1}"/>是
              </label>
              <label class="check-box">
                <input type="radio" name="topFlag" value="0" th:checked="*{topFlag == 0}"/>否
              </label>
            </div>
          </div>
          <!-- 备注-->
          <div class="form-group col-sm-12 pb20">
            <label class="col-xs-2 control-label entering-pd">备注：</label>
            <div class="col-xs-4">
              <input th:field="*{remark}" placeholder="请输入" class="form-control " type="text"/>
            </div>
          </div>
        </div>
      </div>
      <div class="col-sm-2">
        <!--按钮-->
        <div class="col-sm-12 entering-bt" style="margin-top: 10px;position: fixed;">
          <button type="button" class="btn btn-sm btn-info" onclick="doSubmit()">保存
          </button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <button type="button" class="btn btn-sm btn-warning" onclick="closeItem()">关闭
          </button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </div>
      </div>
    </div>
  </form>
</div>

<script th:src="@{/ajax/libs/layui/laydate.js}"></script>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: summernote-js"/>
<script>
  var prefix = "/system/mInterior/cfNotice";

  $(document).ready(function () {
    var roleId=$("#objRole").val()
    var deptId=$("#objDept").val()
    var tell = $("input[name='tell']").val();
    var arChkSons = document.getElementsByName("roleIds")
    if((roleId=="-1" && tell==1) || (deptId=="-1" && tell==2)) {
        document.getElementById('chkAll').checked = true;
        $('#chkAll').parent().attr('class','icheckbox-blue checked')
        for (let i = 0; i < arChkSons.length; i++) {
            if (arChkSons[i].checked != 'true') {
                arChkSons[i].checked = true
            }
        }
    }
    $('.summernote').summernote({
      lang: 'zh-CN',
      tabsize: 2,
      height: 150,//高度
      followingToolbar: false,
      callbacks: {
          onKeydown: function (e) {
              e.stopPropagation();
          },
          onImageUpload: function (files) {
            sendFile(files[0], this);
          }
      }
    });
  });

  function sendFile(file, obj) {
    var data = new FormData();
    data.append("file", file);
    $.ajax({
      type: "POST",
      url: ctx + "common/upload",
      data: data,
      cache: false,
      contentType: false,
      processData: false,
      dataType: 'json',
      success: function(result) {
        if (result.code == web_status.SUCCESS) {
          $(obj).summernote('editor.insertImage', result.url, result.fileName);
        } else {
          $.modal.alertError(result.msg);
        }
      },
      error: function(error) {
        $.modal.alertWarning("图片上传失败。");
      }
    });
  }

  function doSubmit(){
    if(document.getElementById("chkAll").checked){
      $("#objRole").val("-1");
    }
    else{
      var ids="";
      var arrSons = document.getElementsByName("roleIds");
      for (var i=0; i<arrSons.length; i++) {
        if (arrSons[i].checked){
          ids += arrSons[i].value + ",";
        }
      }
      $("#objRole").val(ids.substring(0, ids.length-1));
    }

    if($.validate.form()) {
      var url = prefix + '/afficheEdit';
      var data = $('#form-user-add').serializeArray();
        if($("input[name='tell']").val() == 2){
            let chkVal = $("#objRole").val();
            $.each(data,function (index) {
                if(data[index].name == 'objDept'){
                    data[index].value = chkVal;
                }
                if(data[index].name =='objRole'){
                    data[index].value = '';
                }
            })
        }
      $.operate.saveTab(url,data);
    }
  }

  //搜索
  $('#tellTo').on('click','#dept-search',function (){
      $('#allIn').hide();
      var deptList = $('.find-list').find('li');
      var deptSearch = $('#deptSearch').val();
      $.each(deptList,function (index,value){
          var val = $(value).text();
          var item = $(deptList[index]);//下标
          if(deptSearch === ''){
              $('#allIn').show();
              item.show();
          }else if(val.indexOf(deptSearch) === -1){
              item.hide();
          }else{
              item.show();
          }
      })
      var isShow = true;
      $.each(deptList,function(index,value){
          var item = $(deptList[index]);
          if (item.is(':visible')){
              isShow = false;
          }
      })
      if (isShow) {
          if($(deptList[0]).text() !== '未查询到匹配数据') {
              var oldHtml = $('.find-list').find('ul').html();
              $('.find-list').find('ul').html('<li class="deptTransfer-li">未查询到匹配数据</li>' + oldHtml);
          }else {
              $(deptList[0]).show()
          }
      }else if($(deptList[0]).text() === '未查询到匹配数据'){
          $(deptList[0]).hide()
      }
  })

  $('#chkAll').on('ifClicked',function(obj) {
    var oChkAll = document.getElementById("chkAll");
    var arChkSons = document.getElementsByName("roleIds");
    var tempState= !oChkAll.checked;
    for(var i=0;i<arChkSons.length;i++) {
      if(arChkSons[i].checked != tempState) {
        arChkSons[i].checked = tempState;
      }
    }
  })

  $('#form-user-add').validate({
    rules: {
      sortCode: {
        required: true,
        min: 1
      }
    },
    messages: {
      sortCode: {
        min: ''
      }
    }
  });

  laydate.render({
    elem: '#issueTime',
    type: 'datetime',
    trigger: 'click'
  });
</script>
</body>
</html>

